<?php

namespace Beerus\Modules\VirtualPhone\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use App\Http\Controllers\Controller;
use Encore\Admin\Grid\Displayers\DropdownActions;
use hhz\constant\basic\UserExtendPhoneScene;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Doraemon\cache\UserNick;
use Doraemon\model\VirtualPhone\DesignerVirtualPhoneRecord;
use Beerus\Modules\VirtualPhone\Actions\ReleaseVirtualPhone;
use Beerus\Modules\VirtualPhone\Actions\UpdateVirtualInfo;
use Doraemon\service\BusinessUser\EncryptPhone;
use Beerus\Modules\VirtualPhone\Actions\ShowRealPhone;

/**
 * Undocumented class
 */
class DesignerVirtualListController extends Controller
{
    const PAGE_SIZE = 20;

   const DATA_STATUS_MAP = [
       1 => "正常",
       9 => "删除",
   ];

    public function __construct()
    {
        parent::__construct();
        //隐藏新增按钮--避免隐藏动画出现
        // Admin::style('.grid-create-btn{display:none;}');
        //移除新增按钮
        // Admin::script('$('.pjax-container').attr('id','pjax-container');$('.grid-create-btn').remove();');
    }

    public function index(Request $request, Content $content)
    {
        $request_data = $request->all();

        $page_size = $request->input('per_page', self::PAGE_SIZE);
        $page = $request->input('page', 1);

        $condition = [];
        if (!empty($request_data['designer_uid'])) {
            $condition['designer_uid'] = $request_data['designer_uid'];
        }
        if (!empty($request_data['uid'])) {
            $condition['user_uid'] = $request_data['uid'];
        }
        if (!empty($request_data['user_phone'])) {
            $condition['hidden_user_phone'] = EncryptPhone::getHiddenPhone($request_data['user_phone']);
        }
        if (!empty($request_data['designer_phone'])) {
            $condition['hidden_designer_phone'] = EncryptPhone::getHiddenPhone($request_data['designer_phone']);
        }
        if (!empty($request_data['virtual_phone'])){
            $condition['virtual_phone'] = $request_data['virtual_phone'];
        }

        $condition['ORDER'] = ['id' => 'DESC'];
        $condition['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size
        ];
        //列表查询
        $lists = DesignerVirtualPhoneRecord::select(['id', 'user_uid', 'designer_uid', 'designer_area_name',
            'virtual_phone','virtual_phone_area','virtual_expire_time',
            'data_status','hidden_user_phone','hidden_designer_phone',], $condition);

        $total = 0;
        if (!empty($lists)) {
            unset($condition['ORDER']);
            unset($condition['LIMIT']);
            $total = DesignerVirtualPhoneRecord::count($condition);
        }

        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size
        ]);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) { //传入参数， 避免多次遍历
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->setActionClass(DropdownActions::class);

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('designer_uid', '设计师uid')->placeholder('请输入设计师uid');
                        $filter->equal('uid', '用户uid')->placeholder('请输入用户的uid');
                    });

                    $filter->column(1 / 2, function ($filter) {
                        // 设置datetime类型
                        $filter->equal('user_phone', '用户手机号')->placeholder('请输入用户手机号');
                        $filter->equal('designer_phone', '设计师手机号')->placeholder('请输入设计师手机号');
                        $filter->equal('virtual_phone', '虚拟手机号')->placeholder('请输入虚拟手机号码');
                    });


                });

                $grid->column('id', 'Id');

                $grid->colunm('designer_uid', '设计师uid')->display(function(){
                    return $this->designer_uid;
                });
                $grid->column('designer_nick', '设计师昵称')->display(function () {
                    return UserNick::uid2nick($this->designer_uid);
                });

                $grid->column('decrypt_designer_phone', '设计师手机号码')->display(function () {
                    return $this->hidden_designer_phone;
                });

                $grid->column('designer_area_name', '设计师主要服务城市');

                $grid->column('user_uid', '用户uid');

                $grid->column('user_nick', '用户昵称')->display(function () {
                    return UserNick::uid2nick($this->user_uid);
                });

                $grid->column('hidden_user_phone', '用户真实号码');

                $grid->column('virtual_phone', '虚拟号码');

                $grid->column('virtual_phone_area', '虚拟号码归属地');

                $grid->column('virtual_expire_time', '虚拟号过期时间');

                $grid->column('data_status', '状态')->display(function (){
                    $statusWord = self::DATA_STATUS_MAP[$this->data_status];
                    return $statusWord;
                });
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
                    $actions->disableView();
                    $actions->disableEdit();

                    //处理释放操作
                    //没有`virtual.update`权限的角色不显示强制生效按钮
                    if (Admin::user()->can('virtual.update')) {
                        //$actions->add(new ReleaseVirtualPhone);
                        $actions->add(new UpdateVirtualInfo);
                    }
                    //增加查看用户真实手机号码的逻辑,方便查看
                    if (in_array(Admin::user()->username,["hujingdong","zhangzhijie"])) {
                        //$actions->add(new ReleaseVirtualPhone);
                        $actions->add(new ShowRealPhone);
                    }
                });
            }
        );

        $gridList->disableCreateButton();

        return $content
            ->header('设计师虚拟手机号绑定记录')
            ->row($gridList);
    }
}
